package com.itheima.dubbo.mongodb.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.commons.pojo.Comment;
import com.itheima.commons.vo.PageResult;
import com.itheima.dubbo.interfaces.CommentApi;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @Author: lqTao
 * @Date: 2021/12/9 20:31
 */
@Service
public class CommentApiImpl implements CommentApi {

    @Autowired
    MongoTemplate mongoTemplate;

    //点赞和喜欢都调用这个
    @Override
    public Long comment(Comment comment, Integer type,Class t) {
        //把数据存入mongodb表
        Comment com = mongoTemplate.save(comment);
        //查询对应类型有多少数量
        Long aLong = queryComment(com.getPublishId(), type,t);
        return aLong;
    }

    //取消点赞和喜欢都调用这个
    @Override
    public Long disComment(Long userId, ObjectId publishId, Integer type,Class t) {
        mongoTemplate.remove(Query.query(Criteria.where("userId").is(userId)
                .and("publishId").is(publishId)
        ), Comment.class);
        Long aLong = queryComment(publishId, type,t);
        return aLong;
    }

    //查询点赞和喜欢数量都调用这个
    @Override
    public Long queryComment(ObjectId publishId, Integer type,Class t) {
        long count = mongoTemplate.count(Query.query(Criteria.where("publishId").is(publishId)
                .and("commentType").is(type)), t);
        return count;
    }

    //查询点赞和喜欢状态都调用这个
    @Override
    public boolean queryCommentStatus(ObjectId publishId, Integer type, Long userId,Class t) {
        Object one = mongoTemplate.findOne(Query.query(Criteria.where("userId").is(userId)
                .and("publishId").is(publishId)
        ), t);
        return ObjectUtil.isNotNull(one);
    }

    //查询评论列表
    @Override
    public PageResult queryCommentList(ObjectId publishId, Integer page, Integer pageSize,Class t) {
        List comments = mongoTemplate.find(Query.query(Criteria.where("publishId").is(publishId)
                        .and("commentType").is(2))
                        .with(Sort.by(Sort.Order.asc("created")))
                        .with(PageRequest.of(page, pageSize))
                , t);
        return new PageResult(page, pageSize, queryCommentCount(publishId), comments);
    }

    //查询评论数量
    @Override
    public Long queryCommentCount(ObjectId publishId) {
        long count = mongoTemplate.count(Query.query(Criteria.where("publishId").is(publishId)
                .and("commentType").is(2)
        ), Comment.class);
        return count;
    }


    //发表评论
    @Override
    public Long saveComment(Comment comment) {
        mongoTemplate.save(comment);
        return queryCommentCount(comment.getPublishId());
    }

    //通过主键id查询
    @Override
    public Comment queryComment(ObjectId _id) {
        Comment comment = mongoTemplate.findOne(Query.query(Criteria.where("id").is(_id)), Comment.class);
        return comment;
    }

    @Override
    public List<Comment> queryCommentListByUserId(Integer page, Integer pageSize, Long userId, int commentType) {
        List<Comment> comments = mongoTemplate.find(Query.query(Criteria.where("publishUserId").is(userId)
                .and("commentType").is(commentType))
                .with(Sort.by(Sort.Order.desc("created")))
                .with(PageRequest.of(page - 1, pageSize)), Comment.class);
        return comments;
    }
}
